package c926.xiaoduyun.xiaodu.plugin.signature;


import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.TempFileCache;
import kd.bos.form.control.Html;
import kd.bos.form.control.RichTextEditor;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.util.io.ByteArrayOutputStream;
import kd.sdk.plugin.Plugin;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Entities;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;


import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * 富文本控件测试插件
 */
public class RichTextTestPlugin extends AbstractBillPlugIn implements Plugin {

    /**
     * 富文本新增时点击保存之后
     * 通过大文本字段存储富文本内容
     */
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        FormOperate operate = (FormOperate) e.getSource();
        String key = operate.getOperateKey();

        if (key.equalsIgnoreCase("save")) {
            RichTextEditor edit = this.getView().getControl("c926_richtexteditorap");
            String text = edit.getText();
            this.getModel().setValue("c926_largetextfield_tag", text); // largetextfield大文本字段

//            html2Pdf(text);
        }
    }



    // 打开页面时将大文本值赋给富文本
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        RichTextEditor edit = this.getView().getControl("c926_richtexteditorap");
        String text = (String) this.getModel().getValue("c926_largetextfield_tag");
        edit.setText(text);

        Html html = this.getView().getControl("c926_htmlap");
        html.setConent((String) this.getModel().getValue("c926_largetextfield_tag"));
    }



//    private void html2Pdf(String text) {
//        // 处理 HTML
//        Document doc = Jsoup.parse(text);
//        doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
//        doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
//
//        // 替换字体名称
//        for (Element element : doc.getElementsByAttribute("style")) {
//            String style = element.attr("style");
//            for (Map.Entry<String, String> entry : FONT_FAMILY_MAP.entrySet()) {
//                if (style.contains(entry.getKey())) {
//                    element.attr("style", style.replace(entry.getKey(), entry.getValue()));
//                }
//            }
//        }
//        String html = doc.html();
//
//        // 导出 PDF
//        ITextRenderer renderer = new ITextRenderer();
//        renderer.setDocumentFromString(html);
//        // 设置PDF页面尺寸
//        renderer.setDocumentFromString(html, String.valueOf(PageSize.A4));
//
//        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
//            ITextFontResolver fontResolver = renderer.getFontResolver();
//
//            // 直接使用本地字体路径
//            fontResolver.addFont("jar:"+Thread.currentThread().getContextClassLoader().getResource("simsun.ttc").getPath()
//                    , "SimSun", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, null);
//
//            renderer.layout();
//            renderer.createPDF(out);
//            renderer.finishPDF();
//
//            // 写入临时文件，上传文件服务器
//            TempFileCache tempFileCache = CacheFactory.getCommonCacheFactory().getTempFileCache();
//            String url = tempFileCache.saveAsUrl("test.pdf", out.toByteArray(), 3600);
//            this.getView().download(url);
//        } catch (Exception ex) {
//            this.getView().showErrorNotification("导出失败:" + ex.getMessage());
//        }
//    }

    private final Map<String, String> FONT_FAMILY_MAP = new HashMap<String, String>(){{
        put("宋体", "SimSun");
    }};

}